package org.example.struct.array;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/7/17 15:44
 * @apiNote
 */

public class Test00027RemoveElement {
    public static void main(String[] args) {
        int[] a = new int[]{1, 1, 1, 4, 5, 6};
        int count = test1(a,1);
        System.out.println(count + "  array:" + JSON.toJSON(a));    }

    /**
     * 双指针  ，只需要在数组前保持正确   后面的数据不动
     * @param nums
     * @param val
     * @return
     */
    private static int test1(int[] nums, int val) {
        int slow = 0;
        int fast = 0;
        while(fast<nums.length){
            if(nums[fast] != val){
                nums[slow] = nums[fast];
                slow ++;
            }
            fast ++;
        }
        return slow;
    }

}
